<?php
class order extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->tit = "订单";
		$this->current = 'order';

		$this->orderObj = D('company_order');
		$this->order_logObj = D('company_order_log');

		$this->score_logObj = D('company_score_log');
		$this->money_logObj = D('company_money_log');

		$this->areaObj = D('company_area');
		$this->cateObj = D('company_cate');
		$this->zoneObj = D('company_zone');
		$this->deviceObj = D('company_device');
		$this->adminObj = D('company_admin');

		$this->shopObj = D('company_shop');
		$this->shops = $this->shopObj->get_shop_data($this->company['id'],$this->admin_id);
		// dump($this->shops);exit;
		// $this->smsObj = D('sms');

		$this->auth_list = $this->adminObj->get_auth_list($this->admin);
		$this->areas = $this->auth_list['areas'];
		$this->cates = $this->auth_list['cates'];
		$this->zones = $this->auth_list['zones'];
		// dump($this->auth_list);exit;
		
		$this->config = $this->company_setting['baoxiu'];
	}


	public function index(){
		$this->tab = $this->tab?$this->tab:'1';
		$arrBase = array('tab' => $this->tab,'page' => '0000',);
		if($this->keyword){
			$arrBase = array_merge($arrBase,array('keyword' => $this->keyword));
		}
		$this->nextpage = url($this->routeUrl,'loaddata', $arrBase);

	}


	//loaddata
	public function loaddata(){
		
		$cond = "status=1 ".$this->cond_base;
		// 加入查询条件
		if($this->tab){
			switch($this->tab){
				case '8':
					$cond .=" and order_status=99";
					break;
				default:
					$cond .=" and order_status={$this->tab}";
			}
		}
		//权限
		switch($this->admin['level']){
			case '0'://管理员
				$cond .=" ";
				break;
			case '1'://报修员
				$cond .=" and (admin_id={$this->admin_id} or shifu_id={$this->admin_id})";
				break;
			case '2'://报修组长
				$cond .=" ".$this->cond_cate.$this->cond_zone;
				break;
			case '3'://维修师傅
				$cond .=" and shifu_id={$this->admin_id}";
				break;
			case '4'://维修组长
				$cond .=" ".$this->cond_cate.$this->cond_zone;
				break;
		}

		//搜索
		if($this->keyword){
			$cond.=" and (title like '%".$this->keyword."%' or intro like '%".$this->keyword."%' and local like '%".$this->keyword."%') ";
		}

		$result = $this->orderObj->getList($cond);
        if($result){
			$lists = $result['list'];
			
			// 
        	// foreach ($lists as $val) {
			// 	if($val['photo']){
			// 		$photos = explode(',', $val['photo']);
			// 		$val['img'] = $photos[0];
			// 	}
			// 	$list[] = $val;
			// }
        	// $this->list = $list;
			// $this->showPage = $result['showPage'];
			// $this->total = $result['total'];
			
			$this->list = $lists;

			$this->areas = $this->areaObj->ids($lists,'area_id');
			$this->cates = $this->cateObj->ids($lists,'cate_id');
			$this->zones = $this->zoneObj->ids($lists,'zone_id');
			$this->devices = $this->deviceObj->ids($lists,'device_id');
			$this->admins = $this->adminObj->ids($lists,'admin_id');
			$this->shifus = $this->adminObj->ids($lists,'shifu_id');
			$this->shops = $this->shopObj->ids($lists,'shop_id');
		}else{
			die('0');
		}

	}


	public function search(){
		// $this->cate = $this->cateObj->find(array("url='{$url}' and status=1"));
	}


	public function add(){

		if($this->fmdo == 'add'){

			if(!$this->admin['is_vip']){
				$this->renderError('请先升级会员VIP');
			}
			if($this->admin['vip_time'] < date('Y-m-d')){
				$this->renderError('会员VIP时间已到期');
			}

			$order_status = 1;
			$shop_id = arg('shop_id');
			$money = arg('money');
			$hongbao = arg('hongbao');

			if($shop_id){
				$shop = $this->shopObj->get_detail($shop_id);
				if(!$shop){
					$this->renderError('门店不存在');
				}
			}else{
				$this->renderError('请选择门店');
			}

			if($hongbao){
				$money2 = $money + $hongbao;
			}else{
				$money2 = $money;
			}

			if($this->admin['money'] < $money2){
				$this->renderError('账户金额不足'.$money2.'元');
			}

			if($money < 10){
				$this->renderError('金额不能小于10元');
			}

			$score = intval($money2 / 10);
			if($this->admin['score'] < $score){
				$this->renderError('账户点数不足'.$score.'点');
			}

			$photo = arg('photo');
			if($photo){
				$photo = joinStr(arg('photo'));
			}else{
				$photo = $shop['photo'];
			}

			$arrBase = array(
				'company_id' => $this->company['id'],
				'admin_id' => $this->admin_id,
				'shop_id' => $shop['id'],
				'title' => arg('title'),
				'area_id' => $shop['area_id'],
				'cate_id' => $shop['cate_id'],
				'zone_id' => $shop['zone_id'],
				'money' => $money,
				'score' => $score,
				'hongbao' => $hongbao,
				'map' => arg('map'),
				'intro' => arg('intro'),
				'photo' => $photo,
				'order_status' => $order_status,
				'add_time' => date('Y-m-d H:i:s'),
				'up_time' => $up_time,
			);
			
			$order_id = $this->orderObj->add($arrBase);
			if($order_id){

				//log
				$data['order_status'] = $order_status;
				$this->order_logObj->add_log($order_id,$data);

				//扣除金额
				$arrBase = array(
					'company_id' => $this->company['id'],
					'admin_id' => $this->admin_id,
					'type' => 0,
					'money' => - $money,
					'msg' => '发布订单#'.$order_id,
				);
				$this->money_logObj->add_log($arrBase);
				$this->adminObj->add_money($this->admin_id, - $money);

				//扣除点数
				$arrBase = array(
					'company_id' => $this->company['id'],
					'admin_id' => $this->admin_id,
					'type' => 0,
					'score' => - $score,
					'msg' => '发布订单#'.$order_id,
				);
				$this->score_logObj->add_log($arrBase);
				$this->adminObj->add_score($this->admin_id, - $score);


				//发送短信
				// $this->orderObj->sendsms($order_id,$order_status,$this->config['sms']);
			}
			
			$this->renderSuccess('提交成功',"/{$this->routeUrl}/index?tab=".$order_status);
		}

		//扫码
		//
		$this->device_id = arg('device_id');
		if($this->device_id){
			$this->device = $this->deviceObj->get_detail($this->device_id);
			$this->display($this->routeUrl."/device_set.php");
			exit;
		}

		// if($this->admin['sorce'] <= 10){
		// 	$this->renderError('点数不足,请接单或联系管理员');
		// }

		$this->info = array('status'=>1);
		$this->display($this->routeUrl."/set.php");
		
	}

	public function edit(){

		$id = arg('id');
		if($this->fmdo == 'edit'){

			// $this->renderError('订单暂不支持修改');

			$order_status = 1;
			$shop_id = arg('shop_id');
			$money = arg('money');

			if($shop_id){
				$shop = $this->shopObj->get_detail($shop_id);
				if(!$shop){
					$this->renderError('门店不存在');
				}
			}else{
				$this->renderError('请选择门店');
			}

			$photo = arg('photo');
			if($photo){
				$photo = joinStr(arg('photo'));
			}else{
				$photo = $shop['photo'];
			}

			$arrBase = array(
				'company_id' => $this->company['id'],
				'admin_id' => $this->admin_id,
				'shop_id' => $shop['id'],
				'title' => arg('title'),
				'area_id' => $shop['area_id'],
				'cate_id' => $shop['cate_id'],
				'zone_id' => $shop['zone_id'],
				// 'money' => $money,
				// 'score' => $score,
				'map' => arg('map'),
				'intro' => arg('intro'),
				'photo' => $photo,
				'order_status' => $order_status,
				// 'add_time' => date('Y-m-d H:i:s'),
				'up_time' => date('Y-m-d H:i:s'),
			);
			
			$order_id = $this->orderObj->edit($id,$arrBase);
			if($order_id){

				//log
				$data['order_status'] = $order_status;
				$this->order_logObj->add_log($order_id,$data);

				// //扣除金额
				// $arrBase = array(
				// 	'company_id' => $this->company['id'],
				// 	'admin_id' => $this->admin_id,
				// 	'type' => 0,
				// 	'money' => - $money,
				// 	'msg' => '发布订单#'.$order_id,
				// );
				// $this->money_logObj->add_log($arrBase);
				// $this->adminObj->add_money($this->admin_id, - $money);

				// //扣除点数
				// $arrBase = array(
				// 	'company_id' => $this->company['id'],
				// 	'admin_id' => $this->admin_id,
				// 	'type' => 0,
				// 	'score' => - $score,
				// 	'msg' => '发布订单#'.$order_id,
				// );
				// $this->score_logObj->add_log($arrBase);
				// $this->adminObj->add_score($this->admin_id, - $score);


				//发送短信
				// $this->orderObj->sendsms($order_id,$order_status,$this->config['sms']);
			}
			
			$this->renderSuccess('编辑成功',"/{$this->routeUrl}/index?tab=".$order_status);
		}

		$this->info = $this->orderObj->get_detail($id);
		$this->photos = $this->info['photos'];
		$this->display($this->routeUrl."/set.php");
		
	}

	//详情
	public function detail(){
		$id = arg('id');
		$info = $this->orderObj->get_detail($id); 
		if($info){
			$this->info = $info;
			
			if($info['pj_star1']){
				for ($i=1; $i <= $info['pj_star1']; $i++) { 
					$pj_star1[$i] = 'checked';
				}
				for ($i=1; $i <= $info['pj_star2']; $i++) { 
					$pj_star2[$i] = 'checked';
				}
				$this->pj_star1 = $pj_star1;
				$this->pj_star2 = $pj_star2;
			}
		}
	}

	//操作订单
	public function order_act(){
		$order_status = arg('order_status');
		$order_id = arg('id');
		if($this->fmdo == 'order_act'){

			if(!$this->admin['is_vip']){
				$this->renderError('请先升级会员VIP');
			}
			if($this->admin['vip_time'] < date('Y-m-d')){
				$this->renderError('会员VIP时间已到期');
			}

			$info = $this->orderObj->get_detail($order_id);

			$data = $this->postData('data');
			$data['photo'] = arg('photo');

			if($data['order_status'] !=5 && $data['order_status'] !=99 ){
				if($info['admin_id'] == $this->admin['id']){
					$this->renderError('不能接自己的订单');
				}
			}	
			if($data['order_status'] == 2){
				$data['shifu_id'] = $this->admin['id'];
			}
			$result = $this->orderObj->order_act($order_id, $data);
			if($result){
				//log
				$this->order_logObj->add_log($order_id,$data);
				//发送短信给师傅
				// $this->smsObj->send_user_sms($order_id,$this->company_setting['sms'],'company');

				// 取消返还金额
				if($data['order_status']==99){

					$money = $info['money'];
					if($info['hongbao']){
						$money = $money + $info['hongbao'];
					}
					$score = $info['score'];
					//返还金额
					$arrBase = array(
						'company_id' => $this->company['id'],
						'admin_id' => $this->admin_id,
						'type' => 0,
						'money' => $money,
						'msg' => '取消订单#'.$order_id,
					);
					$this->money_logObj->add_log($arrBase);
					$this->adminObj->add_money($this->admin_id, $money);

					//返还点数
					$arrBase = array(
						'company_id' => $this->company['id'],
						'admin_id' => $this->admin_id,
						'type' => 0,
						'score' => $score,
						'msg' => '取消订单#'.$order_id,
					);
					$this->score_logObj->add_log($arrBase);
					$this->adminObj->add_score($this->admin_id, $score);
				}

				// 确认订单结算
				if($data['order_status']==5){

					$money = $info['money'];
					if($info['hongbao']){
						$money = $money + $info['hongbao'];
					}
					$score = intval($info['score'] * 0.8);

					$order_id = $info['id'];
					//增加金额
					$arrBase = array(
						'company_id' => $this->company['id'],
						'admin_id' => $info['shifu_id'],
						'type' => 0,
						'money' => $money,
						'msg' => '完成订单#'.$order_id,
					);
					$this->money_logObj->add_log($arrBase);
					$this->adminObj->add_money($info['shifu_id'], $money);

					//增加点数
					$arrBase = array(
						'company_id' => $this->company['id'],
						'admin_id' => $info['shifu_id'],
						'type' => 0,
						'score' => $score,
						'msg' => '完成订单#'.$order_id,
					);
					$this->score_logObj->add_log($arrBase);
					$this->adminObj->add_score($info['shifu_id'], $score);
				}

				$this->renderSuccess('操作成功','/'.$this->routeUrl.'/index?tab='.$data['order_status']);
			}
		}

		$act_arr = array(
    		'2'=>'重派订单',
    		'3'=>'接受订单',
    		'4'=>'订单完成',
    		'5'=>'确认完成',
    		'6'=>'评价订单',
    		'7'=>'结算订单',
    		'99'=>'取消订单',
    	);
    	$this->act_name = $act_arr[$order_status];
    	$this->order_status = $order_status;

		$this->info = $this->orderObj->get_detail($order_id);

	}


	//评价
	public function pingjia(){
		$order_id = arg('id');
		if($this->fmdo == 'pingjia'){
			$data = $this->postData('data');

			$order_status = 6;
			$arrBase = array(
				'pj_star1' =>arg('pj_star1'),
				'pj_star2' =>arg('pj_star2'),
				'pj_info' =>arg('pj_info'),
				'order_status' =>$order_status,
				'up_time' =>date('Y-m-d H:i:s'),
				'status' =>1,
			);
			$result = $this->orderObj->edit($order_id, $arrBase);
			if($result){
				//log
				$data['order_status'] = $order_status;
				$this->order_logObj->add_log($order_id,$data);
				//发送短信给师傅
				// $this->smsObj->send_user_sms($order_id,$this->company_setting['sms'],'company');

				$this->renderSuccess('评价成功','/'.$this->routeUrl.'/index?tab='.$data['order_status']);
			}
		}

		$this->info = $this->orderObj->get_detail($order_id);
	}

	//查看评价
	public function pingjia_look(){
		$id = arg('id');
		$info = $this->orderObj->get_info($id);
		
		if($info['pj_star1']){
			for ($i=1; $i <= $info['pj_star1']; $i++) { 
				$pj_star1[$i] = 'checked';
			}
			for ($i=1; $i <= $info['pj_star2']; $i++) { 
				$pj_star2[$i] = 'checked';
			}
			$this->pj_star1 = $pj_star1;
			$this->pj_star2 = $pj_star2;
		}

		$this->info = $info;
	}


	//结算
	function jiesuan(){
		$id = arg('id');
		if($this->fmdo == 'jiesuan'){
			$order_status = 6;
			$arrBase = array(
				// 'yongjin'=>arg('yongjin'),
				// 'yj_money'=>arg('yj_money'),
				'order_status' =>$order_status,
				'up_time' =>date('Y-m-d H:i:s'),
				'status' =>1,
			);
			$this->modelObj->update(array('id'=>$id), $arrBase);
			
			$data = array(
				'msg'=>'佣金已结算',
				'intro'=>arg('intro'),
				'photo'=>joinStr(arg('photo')),
				'yongjin'=>arg('yongjin'),
				'yj_money'=>arg('yj_money'),
			);
			$this->order_logObj->add_log($order_id,$data);

			//发送短信
			D('sms')->send_web_sms($order_id);

			$this->ajaxMsg('信息结算成功',1);
		}

		$info = $this->modelObj->findById($id);
		// $info['yj_money'] = $info['jiage'] * 0.4;
		$this->info = $info;
	}

	

	public function ajax_child(){
		$data['type'] = arg('type');
		$data['id'] = arg('id');
		$data[$data['type'].'_ids'] = $this->admin[$data['type'].'_ids'];
		
		$result = $this->orderObj->get_child($data);
		if($result){
			$this->info = $result['info'];
			$this->list = $result['list'];
			$this->type = $data['type'];
		}

	}

}